<?php
/**
 * Created by PhpStorm.
 * User: PanChaoZhi
 * Date: 2017/1/24
 * Time: 14:32
 */
namespace core;

use core\lib\route;

class Run
{
    public $assign;
    public static $classMap = array();

    static public function run()
    {
        //debug('OK');
        $route = new route();
        //debug($route);
        $controller = $route->controller;
        $action = $route->action;
        $ctrl_file = APP.'/controller/'.$controller.'Controller'.'.php';
        //debug($ctrl_file);
        $controllerClass = '\app\controller\\'.$controller.'Controller';
        //debug($controllerClass);
        //debug(file_exists($ctrl_file));
        if (file_exists($ctrl_file)) {
            include $ctrl_file;
            $controller = new $controllerClass();

            $controller->$action();
        } else {
            throw new \Exception("没有找到控制器文件" . $controller);
        }
    }

    /**
     * 自动加载类库文件，引入路由
     * @param $class
     * @return bool
     */
    static public function autoload($class)
    {
        if (isset($classMap[$class])) {
            return true;
        } else {
            $file = MyFrame . '/' . str_replace('\\', '/', $class);
            if (is_file($file . '.' . 'php')) {
                include $file . '.' . 'php';
                self::$classMap[$class] = $class;
            } else {
                return false;
            }
        }
    }

    public function assign($name, $value)
    {
        $this->assign[$name] = $value;
    }

    public function display($filename)
    {
        $file = APP . '/views/' . $filename;
        if (is_file($file)) {
            extract($this->assign);
            include $file;
        }
    }
}